package br.com.news.activity;

import java.util.Date;
import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import br.com.news.R;
import br.com.news.modal.NoticiaBean;
import br.com.news.util.DataUtil;

public class NoticiaAdapter extends ArrayAdapter<NoticiaBean>{
	
	private List<NoticiaBean> noticiaBeans;
	private boolean caregandoInicio;
	private boolean carregandoFim;
	private boolean maisNoticia;

	public NoticiaAdapter(Context context, int textViewResourceId, List<NoticiaBean> noticiaBeans, boolean caregandoInicio, 
			boolean carregandoFim, boolean maisNoticia) {
		this(context, textViewResourceId, noticiaBeans);
		this.caregandoInicio = caregandoInicio;
		this.carregandoFim = carregandoFim;
		this.maisNoticia = maisNoticia;
	}
	
	private NoticiaAdapter(Context context, int textViewResourceId, List<NoticiaBean> noticiaBeans) {
		super(context, textViewResourceId, noticiaBeans);
		this.noticiaBeans = noticiaBeans;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		
		LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);

		if (caregandoInicio && position == 0) {
			convertView = inflater.inflate(R.layout.item_carregando, null);
		} else if (maisNoticia && position == noticiaBeans.size()-1) {
			convertView = inflater.inflate(R.layout.mais_noticias, null);
		} else if (carregandoFim && position == noticiaBeans.size()-1) {
			convertView = inflater.inflate(R.layout.item_carregando, null);
		} else {
			NoticiaBean noticiaBean = noticiaBeans.get(position);
			convertView = inflater.inflate(R.layout.activity_item_list_noticia, null);

			TextView txtTitle = (TextView) convertView.findViewById(R.activity_item_list_noticia.txtItemTitle);
			txtTitle.setText(noticiaBean.getTitulo());

			TextView txtFonte = (TextView) convertView.findViewById(R.activity_item_list_noticia.txtItemFonte);

			long dif = DataUtil.diferencaDatas(noticiaBean.getDataNoticia(), new Date());
			txtFonte.setText(String.format("%s - %s %s", noticiaBean.getDescricaoFonte(),  DataUtil.mostrarDif(dif, getContext()), getContext().getString(R.string.atras)));
		}

		return convertView;
	}

	public final boolean isCaregandoInicio() {
		return caregandoInicio;
	}

	public final void setCaregandoInicio(boolean caregandoInicio) {
		this.caregandoInicio = caregandoInicio;
	}

	public final void setCarregandoFim(boolean carregandoFim) {
		this.carregandoFim = carregandoFim;
	}

	public final void setMaisNoticia(boolean maisNoticia) {
		this.maisNoticia = maisNoticia;
	}
	
	public final boolean isCarregandoFim() {
		return carregandoFim;
	}

	public final boolean isMaisNoticia() {
		return maisNoticia;
	}

}
